package com.faf.ui;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class EditCart
 */
@WebServlet("/EditCart")
public class EditCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int fid = Integer.valueOf(request.getParameter("fid").toString());
		int action = Integer.valueOf(request.getParameter("action").toString());
		
		response.setContentType("text/html;charset=utf-8"); 
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		HashMap<Integer, Integer> global_info_cart_id2count = (HashMap<Integer, Integer>)request.getSession().getAttribute("global_cart");
		
		int t_fnum = global_info_cart_id2count.get(fid);
		if(action == 0) {
			// remove
			if(t_fnum == 1) {
				response.getWriter().write("{\"res\":\"Fail\"}");
				return;
			}
			else
				global_info_cart_id2count.put(fid, t_fnum-1);
		}
		else {
			// add
			t_fnum ++;
			global_info_cart_id2count.put(fid, t_fnum);
		}
		
		request.getSession().setAttribute("global_cart", global_info_cart_id2count);
		
		response.getWriter().write("{\"res\":\"Succ\"}");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
